package dmir.reaction.publico.graph.gephi;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.gephi.data.attributes.api.AttributeColumn;
import org.gephi.data.attributes.api.AttributeController;
import org.gephi.data.attributes.api.AttributeModel;
import org.gephi.graph.api.Attributes;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.Node;
import org.gephi.graph.api.UndirectedGraph;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.gephi.statistics.plugin.ClusteringCoefficient;
import org.gephi.statistics.plugin.Degree;
import org.gephi.statistics.plugin.GraphDistance;
import org.gephi.statistics.plugin.Hits;
import org.gephi.statistics.plugin.PageRank;
import org.openide.util.Lookup;

public class EntityCharacterization {

	static Workspace workspace;
	static ImportController importController;
	static GraphModel graphModel;
	
	public static void start(CommandLine line) {
	    
		//Init a project - and therefore a workspace
	    ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
	    pc.newProject();
	    
	    //Get controllers and models
		Workspace workspace = pc.getCurrentWorkspace();
		ImportController importController = Lookup.getDefault().lookup(ImportController.class);
		GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
		AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel();
		
		//Import file
	    Container container;
	    try {
	        container = importController.importFile(new File(line.getOptionValue("inputgraph")));
	    } catch (Exception ex) {
	        ex.printStackTrace();
	        return;
	    }

	    //Append imported data to GraphAPI
	    importController.process(container, new DefaultProcessor(), workspace);
	    	    
		System.out.print(line.getOptionValue("inputgraph").split(".graphml")[0] + '\t');		
		String entity = line.getOptionValue("id");
		
		final UndirectedGraph graph = graphModel.getUndirectedGraph();
	        
		
		/*	Frequency	Degree	PageRank	Number of Triangles	Cluster Coefficient	Authority	Hub	*/

		Node n = graph.getNode(entity);
		
		if (n!=null) {
			Attributes attr = n.getNodeData().getAttributes();
			
			/*
			System.out.println("id : " 		+ attr.getValue(0));
			System.out.println("label: " 	+ attr.getValue(1));
			System.out.println("etype: " 	+ attr.getValue(2));
			System.out.println("ename:  "   + attr.getValue(3));
			System.out.println("freq: " 	+ attr.getValue(4));
			*/
					
			Degree degree = new Degree();
			System.out.println("Calculating degrees...");			
			degree.execute(graphModel, attributeModel);
			
			/*
			GraphDistance distance = new GraphDistance();
			distance.execute(graphModel, attributeModel);
			*/
			
			Hits hits = new Hits();
			System.out.println("Calculating HITS...");
			hits.execute(graphModel, attributeModel);
			
			PageRank pagerank = new PageRank();
			System.out.println("Calculating PageRank...");
			pagerank.execute(graphModel, attributeModel);
			
			ClusteringCoefficient clustercoefficient = new ClusteringCoefficient();
			clustercoefficient.execute(graphModel, attributeModel);
			
			AttributeColumn freq = attributeModel.getNodeTable().getColumn("freq");		
			AttributeColumn deg = attributeModel.getNodeTable().getColumn(Degree.DEGREE);
			AttributeColumn pag = attributeModel.getNodeTable().getColumn(PageRank.PAGERANK);
			AttributeColumn auth = attributeModel.getNodeTable().getColumn(Hits.AUTHORITY);
			AttributeColumn hub = attributeModel.getNodeTable().getColumn(Hits.HUB);
			
			/*
			AttributeColumn betweeness = attributeModel.getNodeTable().getColumn(distance.BETWEENNESS);
			AttributeColumn closseness = attributeModel.getNodeTable().getColumn(distance.CLOSENESS);
			AttributeColumn eccentricity = attributeModel.getNodeTable().getColumn(distance.ECCENTRICITY);
			*/
			
			int d = (Integer) n.getNodeData().getAttributes().getValue(deg.getIndex());
			int f = (Integer) n.getNodeData().getAttributes().getValue(freq.getIndex());
			double p = (Double) n.getNodeData().getAttributes().getValue(pag.getIndex());
			float a = (Float) n.getNodeData().getAttributes().getValue(auth.getIndex());
			float h = (Float) n.getNodeData().getAttributes().getValue(hub.getIndex());
			
			/*
			double b = (Double) n.getNodeData().getAttributes().getValue(betweeness.getIndex());
			double c = (Double) n.getNodeData().getAttributes().getValue(closseness.getIndex());
			double e = (Double) n.getNodeData().getAttributes().getValue(eccentricity.getIndex());
			*/
					
			//System.out.println("degree: " + d + "\t frequency: " + f + "\t pagerank: " + p + "\tauthority:" + a + "\t" + "hub: " + h + "\t betweeness:" + b + "\t closseness:" + c + "\t eccentricity:" + e);
			//System.out.println( d + "\t" + f + "\t" + p + "\t" + a + "\t" + h + "\t" + b + "\t" + c + "\t" + e);
			System.out.println( d + "\t" + f + "\t" + p + "\t" + a + "\t" + h);
		}
		else  System.out.println( 0 + "\t" + 0 + "\t" + 0 + "\t" + 0 + "\t" + 0 + "\t" + 0 + "\t" + 0 + "\t" + 0);
		
	}	
}